package com.winway.sixbook.custom;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.winway.sixbook.R;
import com.winway.sixbook.service.RecordDataManager;
import com.winway.sixbook.ui.NewRecordAdd;

public class ListViewAdapter extends BaseExpandableListAdapter  {

	private  static final String TAG = "ListViewAdapter";
	private  List<Map<String,Object>> groupArray; //存放组数据
    private  List<List<Map<String,Object>>> childArray;  //存放各组的子数据
    private  Context context;
    protected LayoutInflater mInflater;
    
    public ListViewAdapter(Context context, RecordDataManager recordDataManager ,String addtime){

   	 	this.context=context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //初始化要显示的数据
        groupArray = new ArrayList<Map<String,Object>>();
        childArray = new ArrayList<List<Map<String,Object>>>();
        
        SimpleDateFormat dateformat1=new SimpleDateFormat("yyyy-MM-dd");
        if(null == addtime || "".equals(addtime)){
        	addtime=dateformat1.format(new Date());
        }
        Cursor groupcursoCursor = recordDataManager.findRecordByAddtime(addtime);
        if(null != groupcursoCursor){
            if(groupcursoCursor.moveToFirst() ){
                do{
                	Map<String,Object> group = new HashMap<String,Object>();
                	group.put("grouphour",groupcursoCursor.getString(groupcursoCursor.getColumnIndex("add_hour")));
                	group.put("groupmin", groupcursoCursor.getString(groupcursoCursor.getColumnIndex("add_min")));
                	group.put("grouptype", groupcursoCursor.getString(groupcursoCursor.getColumnIndex("type_name")));
                	group.put("groupisqy", groupcursoCursor.getString(groupcursoCursor.getColumnIndex("isqy")));
                	groupArray.add(group) ;
                }while( groupcursoCursor.moveToNext());
            }else{
            	Map<String,Object> group = new HashMap<String,Object>();
            	group.put("grouphour", "");
            	group.put("groupmin", "");
            	group.put("grouptype", "暂无数据");
            	group.put("groupisqy", "");
            	groupArray.add(group) ;
            }
        }else{
        	Map<String,Object> group = new HashMap<String,Object>();
        	group.put("grouphour", "");
        	group.put("groupmin", "");
        	group.put("grouptype", "暂无数据");
        	group.put("groupisqy", "");
        	groupArray.add(group) ;
        }
        groupcursoCursor.close();
        
        for (int j=0;j<groupArray.size();j++){
        	List<Map<String,Object>> child = new ArrayList<Map<String,Object>>(); 
        	Cursor cursor = recordDataManager.findRecordByTimeslot(addtime,groupArray.get(j).get("grouphour").toString(),groupArray.get(j).get("groupmin").toString());
            if(cursor != null ){
               if( cursor.moveToFirst() ){
                   do{
   			        	Map<String,Object> child1date = new HashMap<String,Object>();
   			        	child1date.put("Content", cursor.getString(cursor.getColumnIndex("content")));
   			        	child1date.put("childtype", cursor.getString(cursor.getColumnIndex("contenttype")));
   			        	child1date.put("recorddataid", cursor.getString(cursor.getColumnIndex("recorddataid")));
   			        	child1date.put("newtypeid",  cursor.getString(cursor.getColumnIndex("type_id")));
   			        	child1date.put("newtypename",  cursor.getString(cursor.getColumnIndex("type_name")));
   			        	child1date.put("isqy",  cursor.getString(cursor.getColumnIndex("isqy")));
   			        	child.add(child1date);
                   }while( cursor.moveToNext());
               }
            }
            cursor.close();
            childArray.add(child);
        }
    }


    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childArray.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }
    public Context getContext() {
		return context;
	}


	public void setContext(Context context) {
		this.context = context;
	}
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        final childView child;
        if(convertView==null){
            convertView = mInflater.inflate(R.layout.childview, null);
            child = new childView();
            child.childtextcontent = (TextView) convertView.findViewById(R.id.childtext);
            child.childtype = (TextView) convertView.findViewById(R.id.childtype);
            child.recorddataid = (TextView) convertView.findViewById(R.id.childid);
            child.newtypeid = (TextView) convertView.findViewById(R.id.newtypeid);
            child.newtypename= (TextView) convertView.findViewById(R.id.newtypename);
            convertView.setTag(child);
        }else{
            child = (childView) convertView.getTag();
        }
        Map<String,Object> child1date =  childArray.get(groupPosition).get(childPosition);
        String childcontent = child1date.get("Content").toString(); 
        String childtype = child1date.get("childtype").toString();
        String recorddataid = child1date.get("recorddataid").toString();
        String newtypeid = child1date.get("newtypeid").toString();
        String newtypename = child1date.get("newtypename").toString();
        String isqy = child1date.get("isqy").toString();
        
        child.childtextcontent.setText(childcontent);
        String typeString = "正面：";
        if(null != childtype && !"".equals(childtype)){
        	if(null != newtypeid && !"".equals(newtypeid)){
        		if(Integer.parseInt(newtypeid)==0){
        			if(Integer.parseInt(childtype) == 1){
                		typeString = "今日最好:";
                	}else if (Integer.parseInt(childtype) == 2) {
                		typeString = "最需改进:";
        			}else if (Integer.parseInt(childtype) == 3) {
        				typeString = "发        愿:";
        			}
        			child.childtextcontent.setPadding(2, 0, 0, 0);
        		}else{
        			if(Integer.parseInt(childtype) == 1){
                		typeString = "正        面:";
                	}else if (Integer.parseInt(childtype) == 2) {
                		typeString = "负        面:";
        			}else if (Integer.parseInt(childtype) == 3) {
        				typeString = "我        要:";
        			}
        			child.childtextcontent.setPadding(2, 0, 0, 0);
        		}
        	}
        }
        child.childtype.setText(typeString);
        child.recorddataid.setText(recorddataid);
        child.newtypeid.setText(newtypeid);
        child.newtypename.setText(newtypename);
        
        child.childtextcontent.setOnClickListener(new View.OnClickListener() {
			@Override
            public void onClick(View v) {
//                Toast.makeText(context, "内容："+child.childtextcontent.getText()+child.recorddataid.getText(), Toast.LENGTH_LONG).show();
            	Intent itentIntent = new Intent(getContext(), NewRecordAdd.class);
            	Bundle bundle = new Bundle(); 
				bundle.putString("typename", ""+child.newtypename.getText());
				bundle.putString("typeid", ""+child.newtypeid.getText());
				bundle.putString("recorddataid", ""+child.recorddataid.getText());
				itentIntent.putExtras(bundle);
            	getContext().startActivity(itentIntent);
            }
        });
        return  convertView;  
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return childArray.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return groupArray.get(groupPosition);  
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return groupArray.size();  
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;  
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupView group;
        if(convertView==null)
        {
            convertView = mInflater.inflate(R.layout.groupview, null);
            group = new GroupView();
            group.grouptext= (TextView) convertView.findViewById(R.id.grouptext);
            group.grouptypetext = (TextView)convertView.findViewById(R.id.grouptypetext);
            convertView.setTag(group);
        }else
        {
            group = (GroupView) convertView.getTag();
        }
        String grouphour = groupArray.get(groupPosition).get("grouphour").toString();  
        String groupmin = groupArray.get(groupPosition).get("groupmin").toString();  
        String grouptype = groupArray.get(groupPosition).get("grouptype").toString();
        String groupisqy = groupArray.get(groupPosition).get("groupisqy").toString();
        
        String groupnameString = "";
        if(null != grouphour && !"".equals(grouphour)){
        	if(Integer.parseInt(grouphour)<10){
        		grouphour = "0"+grouphour+"：";
        	}else{
        		grouphour = grouphour+"：";
        	}
        }
        if(null != groupmin && !"".equals(groupmin)){
        	if(Integer.parseInt(groupmin)<10){
        		groupmin = "0"+groupmin;
        	}
        }
        String isqystrString = "";
        if(null != groupisqy && !"".equals(groupisqy)){
        	if(Integer.parseInt(groupisqy)==1){
        		isqystrString = "[祈愿]";
        	}
        }
        group.grouptext.setText(grouphour+groupmin+isqystrString);
        group.grouptypetext.setText(grouptype);
        return  convertView;  
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return false;
    }
    private class GroupView
    {
        public TextView grouptext;
        public TextView grouptypetext;
    }

    private class childView
    {
        public TextView childtextcontent;
        public TextView childtype;
        public TextView recorddataid;
        public TextView newtypename;
        public TextView newtypeid;
    }

}
